/*
	The MIT License

	Copyright (c) 2007 Mike Chambers

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#import "IURLService.h"
#import "URLTeaService.h"
#import "TinyURLService.h"
#import "BitlyService.h"

//app version
#define VERSION ".93"


//print usage instruction
void printUsage(void)
{
	printf("Usage: turl [-v] [-urltea|tinyurl|bitly] URL\n");
}

//print error information to console
void printError(void)
{
	printf("Error creating URL : Make sure that the syntax is correct and that the service is currently online.\n");
}


//app entry
int main (int argc, const char * argv[])
{
	//check that there are 1 or 2 args passed in (first arg is app name)
	if(argc < 2 || argc > 3)
	{
		//if not print usage
		printUsage();
		return 1;
	}

	//set up autorelease pool
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	NSString *arg2 = [NSString stringWithCString : argv[1] ];

	
	//is there a better way to parse command line args
	if([arg2 compare:@"-v"] == NSOrderedSame)
	{
		//make this a constant
		printf("turl version %s Created by Mike Chambers : mikechambers.com\n", VERSION);
		return 0;
	}

	//all you base are belong to us
	if([arg2 compare:@"-d"] == NSOrderedSame)
	{
		printf("turl is a Desktop 2.0 hybrid application that leverages cloud "
			"based rest apis to optimize urls for distribution to end users\n");
		return 0;
	}


	//the url shortening service we will use (which impliments IURLService protocol)
	id <IURLService> service = nil;
	
	//index of url to shorten
	int urlIndex = 1;
	
	//right now if you pass in unknown service option, default will be used
	//we should check if a service is specified, and then validate it
	
	//check for -urltea argument
	if([arg2 compare:@"-urltea"] == NSOrderedSame)
	{
		//if it is there, use urltea service
		service = [URLTeaService new];
		
		//url is 2nd arg
		urlIndex = 2;
	} //check for tinyurl arg
	else if([arg2 compare:@"-tinyurl"] == NSOrderedSame)
	{
		//if so use tiny url service
		service = [TinyURLService new];
		
		//url is 2nd arg
		urlIndex = 2;
	}
	else if([arg2 compare:@"-bitly"] == NSOrderedSame)
	{
		//if so use tiny url service
		service = [BitlyService new];
		
		//url is 2nd arg
		urlIndex = 2;
	}
	else
	{
		//if no arg is specified, then default to urltea service
		service = [BitlyService new];
	}	
	
	//if url index is 2nd, make sure that there is actually an argument / url there
	if(urlIndex == 2 && argc != 3)
	{
		printUsage();
		return 1;	
	}
	
	//holds url to shorten
	NSString *arg = [NSString stringWithCString : argv[urlIndex]];
	
	//use this to capture any errors when creating the url
	NSError *error;

	//call the service to create the new url
	NSString *out = [service shortenURL: arg error:&error];
	
	
	//make sure that a URL was returned. if not, then something went wrong
	if(out == nil)
	{
		printError();
		return 1;	
	}
	
	//make sure that a URL was actually returned
	if(![out hasPrefix:@"http://"])
	{
		printError();
		return 1;
	}
	
	//write shortened url out to the console
	printf("%s\n", [out UTF8String]);

	//release the memory pool
    [pool release];
    return 0;
}
